package reflectdemo;

import enumdemo.TestEnum;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-08-12
 * Time: 19:00
 */
public class ReflectClassDemo {

    //通过反射，构建Student对象
    public static void reflectNewInstance() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Student student = (Student) c1.newInstance();
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //反射私有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            //建立一个构造方法的对象
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class, int.class);
            constructor.setAccessible(true);

            Student student = (Student) constructor.newInstance("meng", 18);
            System.out.println(student);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //反射私有的属性，这里反射Student的age
    public static void reflectPrivateField() {
        try {
            Class<?> c = Class.forName("reflectdemo.Student");
            Student student = (Student) c.newInstance();
            Field field = c.getDeclaredField("age");
            field.setAccessible(true);
            field.set(student, 25);
            System.out.println(student);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void reflectPrivateMethod() {
        try {
            Class c = Class.forName("reflectdemo.Student");
            Method method = c.getDeclaredMethod("function", String.class);
            Student student = (Student) c.newInstance();
            method.setAccessible(true);
            method.invoke(student, "通过反射调用私有方法");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}